﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UserDefinedForms.Business;
using UserDefinedForms.Entities;
using Microsoft.VisualBasic;

namespace UDFDesign.GUIComponent.SupForm
{
    public partial class SupplierManagerForm : Form
    {
        private List<int> listId;
        private List<String> listName;
        private SupplierService supplierService = new SupplierService();
        private DesignFormService formEntityService = new DesignFormService();
        private SupOrderFormService supOrderFormService = new SupOrderFormService();
        private TList<Supplier> listSupplier= new TList<Supplier>();

        public SupplierManagerForm()
        {

            InitializeComponent();
            
            publishDataToGrid();
        }


        private void initGrid()
        {
            

        }

        private void publishDataToGrid()
        {
            listSupplier = supplierService.GetAll();
            supplierGridView.DataSource = listSupplier;
            supplierGridView.Columns["SupplierID"].Visible = false;
            supplierGridView.Columns["Status"].Visible = false;


            
            if (supplierGridView.Rows.Count > 0)
            {
                supplierGridView.Rows[0].Selected = true;
            }
            


            /*
            formNameGrid.Rows.Clear();
            for (int i = 0; i < listName.Count; i++)
            {
                formNameGrid.Rows.Add(new string[] { "" + (i + 1), listName[i] });
            }
            formNameGrid.Refresh();
          */
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            DesignForm supFormEntity = supFormGridView.SelectedRows[0].DataBoundItem as DesignForm;
            SupplierForm supForm = new SupplierForm(supFormEntity);
            supForm.ShowDialog(this);
            publishDataToGrid();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            /*
            int index = formNameGrid.SelectedRows[0].Index;
            if (index < 0) return;
            int id = listId[index];
            SupplierFormORM currentForm = SystemFactory.getCurrentForm();
            if (currentForm != null && currentForm.Id == id)
            {
                MessageBox.Show("This form is currently in use !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            formService.DeleteForm(id);
            publishDataToGrid();
             * */
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void newSupplierBtn_Click(object sender, EventArgs e)
        {
            String newSupplierName= Microsoft.VisualBasic.Interaction.InputBox("Input Supplier's name?", "New Supplier", "New Supplier");
            Supplier newSupplier = new Supplier();
            newSupplier.SupplierName = newSupplierName;
            newSupplier.Status = 1;
            supplierService.Save(newSupplier);
            publishDataToGrid();
        }

        private void newSupFormBtn_Click(object sender, EventArgs e)
        {
            Supplier sup = supplierGridView.SelectedRows[0].DataBoundItem as Supplier;
            SupplierForm supForm = new SupplierForm(sup.SupplierId);
            supForm.ShowDialog(this);
            publishDataToGrid();
        }

        private void supplierGridSelectionChange(object sender, EventArgs e)
        {
            if (supplierGridView.SelectedRows.Count <= 0) return;
            Supplier sup = supplierGridView.SelectedRows[0].DataBoundItem as Supplier;
            TList<SupOrderForm> listSup= supOrderFormService.GetBySupplierId(sup.SupplierId);
            TList<DesignForm> listSupForm = new TList<DesignForm>();
            foreach (SupOrderForm supO in listSup)
            {
                DesignForm f = formEntityService.GetByFormId(supO.FormId);
                listSupForm.Add(f);
                
            }

            supFormGridView.DataSource = listSupForm;
            supFormGridView.Columns["Status"].Visible = false;
            supFormGridView.Columns["FormID"].Visible = false;
            supFormGridView.Columns["FormType"].Visible = false;
            supFormGridView.Columns["SupOrderForm"].Visible = false;
           // supFormGridView.Update();
            //supFormGridView.Refresh();
        }
    }
}
